package com.github.kezhenxu94.playground.java.interview.netease;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 二进制权重
 * 
 * @author KeZhenxu
 *
 */
public class NetEaseBinaryWeight {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		char[] allBits = new char[32];
		Arrays.fill(allBits, '0');
		char[] binaryDigits = Integer.toBinaryString(n).toCharArray();
		System.arraycopy(binaryDigits, 0, allBits, 32 - binaryDigits.length, binaryDigits.length);

		int lastOneIdx = 31;
		while (allBits[lastOneIdx] == '0' && lastOneIdx >= 0)
			lastOneIdx--;
		if (allBits[lastOneIdx - 1] == '0') {
			allBits[lastOneIdx - 1] = '1';
			allBits[lastOneIdx] = '0';
		} else {
			int oneCount = 0;
			allBits[lastOneIdx] = '0';
			lastOneIdx--;
			while (allBits[lastOneIdx] == '1' && lastOneIdx >= 0) {
				allBits[lastOneIdx] = '0';
				oneCount++;
				lastOneIdx--;
			}
			allBits[lastOneIdx] = '1';
			oneCount--;
			for (int i = 31; oneCount > 0 || i > lastOneIdx; oneCount--, i--)
				if (oneCount >= 0)
					allBits[i] = '1';
				else
					allBits[i] = '0';
		}
		System.out.println(Integer.valueOf(String.format("%s", new String(allBits)), 2));
		sc.close();
	}
}
